package aes.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;

import aes.general.GuiControlBeta;
import aes.general.Question;
import aes.general.QuestionControl;
import aes.general.Teacher;

/**
 * The Class TeacherQuestionPanel. This panel is for the teacher to compose a question
 * from the beginning of writing the question until the
 * final step. After completion, the question is saved
 * and stored in the Data Base. 
 */
public class TeacherQuestionPanel extends JPanel implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel Qidlabel;
	private JLabel Qlabel;
	private JLabel AnswerLabel;
	private JLabel lblQuestionCompose;
	private JPanel cp;
	private JTextField subidText;
	private JTextField AnsText1;
	private JTextField AnsText2;
	private JTextField AnsText3;
	private JTextField AnsText4;
	private JTextField Qidtext;
	private JTextArea Qsttext;
	private JTextPane descText;
	private JButton btnSave;
	private JButton btnCancel;
	private JRadioButton b1 ;
	private JRadioButton b2 ;
	private JRadioButton b3 ;
	private JRadioButton b4 ;
	private ButtonGroup group;
	private JSeparator separator;
	private JComboBox subBox;
	
	private Question qst;
	private ArrayList<String> ans;
	
	/**
	 * Create the panel.
	 *
	 * @param contentPane the content pane
	 */
	public TeacherQuestionPanel(JPanel contentPane) {
		setLayout(null);
		this.cp = contentPane;
		initialize();
		qst = new Question();	
	}

	/**
	 * Initialize.
	 */
	private void initialize() {
		
		group = new ButtonGroup();
	    Qidlabel = new JLabel("Question Number : ");
		Qidlabel.setBounds(7, 92, 117, 14);
		this.add(Qidlabel);
		
		subidText = new JTextField(2);
		subidText.setEditable(false);
		subidText.setBounds(120, 86, 31, 20);
		subidText.setFont(new Font("Tahoma", Font.BOLD, 14));
		this.add(subidText);
		subidText.setColumns(5);
		
		Qlabel = new JLabel("Question :");
		Qlabel.setBounds(7, 117, 67, 14);
		this.add(Qlabel);
		Qsttext = new JTextArea();
		Qsttext.setBounds(7, 142, 504, 75);
		Qsttext.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		add(Qsttext);
		
		AnswerLabel = new JLabel("Answers");
		AnswerLabel.setBounds(7, 331, 98, 14);
		this.add(AnswerLabel);
		
		b1 = new JRadioButton("1");
		b1.addActionListener(this);
		b1.setBounds(7, 352, 31, 23);
		this.add(b1);
		
		b3 = new JRadioButton("3");
		b3.setBounds(226, 352, 31, 23);
		b3.addActionListener(this);
		this.add(b3);
		
		b2 = new JRadioButton("2");
		b2.setHorizontalAlignment(SwingConstants.LEFT);
		b2.setBounds(7, 378, 31, 23);
		b2.addActionListener(this);
		this.add(b2);
		
		b4 = new JRadioButton("4");
		b4.setBounds(226, 378, 31, 23);
		b4.addActionListener(this);
		this.add(b4);
		
		group.add(b1);
		group.add(b2);
		group.add(b3);
		group.add(b4);
		
		AnsText1 = new JTextField();
		AnsText1.setBounds(44, 356, 176, 20);
		this.add(AnsText1);
		AnsText1.setColumns(10);
		
		AnsText2 = new JTextField();
		AnsText2.setBounds(44, 379, 176, 20);
		this.add(AnsText2);
		AnsText2.setColumns(10);
		
		AnsText3 = new JTextField();
		AnsText3.setBounds(263, 353, 176, 20);
		this.add(AnsText3);
		AnsText3.setColumns(10);
		
		AnsText4 = new JTextField();
		AnsText4.setBounds(263, 379, 176, 20);
		this.add(AnsText4);
		AnsText4.setColumns(10);
		
		btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(addQuestion())
				{	
						JOptionPane.showMessageDialog(null, "Question Saved", "OK",
								JOptionPane.INFORMATION_MESSAGE);
						Qidtext.setText(null);
						Qsttext.setText(null);
						descText.setText(null);
						AnsText1.setText(null);
						AnsText2.setText(null);
						AnsText3.setText(null);
						AnsText4.setText(null);
						b1.setSelected(false);
						b2.setSelected(false);
						b3.setSelected(false);
						b4.setSelected(false);
						group.clearSelection();
						subBox.setSelectedIndex(0);
					CardLayout cl = (CardLayout)(cp.getLayout());
					 cl.show(cp,"mainPanel");
				}
				
			}
		});
		btnSave.setBounds(120, 410, 89, 23);
		this.add(btnSave);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Qidtext.setText(null);
				Qsttext.setText(null);
				descText.setText(null);
				AnsText1.setText(null);
				AnsText2.setText(null);
				AnsText3.setText(null);
				AnsText4.setText(null);
				group.clearSelection();
				subBox.setSelectedIndex(0);
				CardLayout cl = (CardLayout)(cp.getLayout());
				 cl.show(cp,"mainPanel");
			}
		});
		btnCancel.setBounds(263, 410, 89, 23);
		add(btnCancel);
		
		lblQuestionCompose = new JLabel("Question Compose");
		lblQuestionCompose.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		lblQuestionCompose.setBounds(214, 11, 212, 29);
		add(lblQuestionCompose);
		
		separator = new JSeparator();
		separator.setBounds(7, 41, 623, 2);
		add(separator);
		
		JLabel lblDescription = new JLabel("Description :");
		lblDescription.setBounds(7, 230, 86, 14);
		add(lblDescription);
		
		descText = new JTextPane();
		descText.setBounds(4, 251, 507, 69);
		descText.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		add(descText);
		
		JLabel subjlbl = new JLabel("Subject :");
		subjlbl.setBounds(7, 55, 67, 14);
		add(subjlbl);
		
		subBox = new JComboBox(); 
		subBox.setBounds(120, 54, 86, 20);
		subBox.addItem("-------");
		for(String subj:Teacher.getSname())
			subBox.addItem(subj);		
		subBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(subBox.getSelectedItem().equals("-------"))
					subidText.setText(null);
				else
				subidText.setText(Teacher.getSid().get(Teacher.getSname().indexOf((String)subBox.getSelectedItem())));
			}
		});	
		add(subBox);
		
		Qidtext = new JTextField();
		Qidtext.setBounds(158, 86, 48, 20);
		Qidtext.setFont(new Font("Tahoma", Font.BOLD, 14));
		add(Qidtext);
		Qidtext.setColumns(3);
		
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == b1)
		 qst.setCorrectAns(1);
		else
			if(e.getSource() == b2)
				 qst.setCorrectAns(2);
			else
				if(e.getSource() == b3)
					 qst.setCorrectAns(3);
				else
					if(e.getSource() == b4)
						 qst.setCorrectAns(4);
	}
	
	/**
	 * Adds the question.
	 *
	 * @return boolean answer that indicates to the teacher
	 * if the question was added correctly or if it wasn't.
	 */
	protected Boolean addQuestion() {
		ans = new ArrayList<String>();
	///////////////////////////////////////////////////////////////////////////////////////	
		if (Qidtext.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,Id is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if (Qidtext.getText().trim().length()!=3) {
			JOptionPane.showMessageDialog(null, "Error,Id is incorrect", "Error",
					JOptionPane.ERROR_MESSAGE);
			Qidtext.setText(null);
			return false;
		}
		try {
			Integer.decode(Qidtext.getText());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Error,Id must be a Number",
					"Error", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		qst.setQId(subidText.getText() + Qidtext.getText());
		
/////////////////////////////////////////////////////////////////////////////////////////	
		if (Qsttext.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,Question text is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		qst.setQuestion(Qsttext.getText());
		
/////////////////////////////////////////////////////////////////////////////////////////		
		if (AnsText1.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,Answer 1 is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		ans.add(AnsText1.getText());
		
/////////////////////////////////////////////////////////////////////////////////////////		
		if (AnsText2.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,Answer 2 is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}
		ans.add(AnsText2.getText());
		
/////////////////////////////////////////////////////////////////////////////////////////		
		if (AnsText3.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,Answer 3 is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
		return false;
		}
    	ans.add(AnsText3.getText());
    	
/////////////////////////////////////////////////////////////////////////////////////////		
		if (AnsText3.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,Answer 3 is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
		return false;
		}
		if (subidText.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,no subject added", "Error",
					JOptionPane.ERROR_MESSAGE);
		return false;
		}
		
		ans.add(AnsText4.getText());
		qst.setAnswer(ans);
		
		if (group.isSelected(null)) {
			JOptionPane.showMessageDialog(null, "Error, Must select an answer", "Error",
					JOptionPane.ERROR_MESSAGE);
		return false;
		}
		qst.setDescription(descText.getText());
		qst.setAuthor(GuiControlBeta.userid);
		qst.setSubject(subidText.getText());
		
		QuestionControl Qc = new QuestionControl(this);
		Qc.AddQuestionrRequestToServer(qst);
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!Qc.getRequest())
		{
			JOptionPane.showMessageDialog(null, "ERROR , QUestion Already Exists.",
					"Error", JOptionPane.ERROR_MESSAGE);
				Qidtext.setText(null);
				return false;
		}
		else
			return true;
		
	}
}
